package leetcode.editor.cn;

import java.util.*;
import leetcode.editor.common.*;

public class SortColors {

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public void sortColors(int[] nums) {
            // p0  表示 0 区间   p2 表示 2区间  中间的就是 1 区间
            // 注意区间
            int p0 = 0;
            int p2 = nums.length - 1;

            // 第三个指针 负责遍历
            int p =0;

            while (p0 <= p2){
                if(nums[p]==0){
                    swap(nums,p0,p);
                    p0++;
                }else if(nums[p]==2){
                    swap(nums,p2,p);
                    p2--;
                }else if(nums[p]==1){
                    p++;
                }
                // 1 要比 0 大
                if(p<p0){
                    p=p0;
                }
            }
        }
    }
    //leetcode submit region end(Prohibit modification and deletion)

    private void swap(int[] nums, int i, int j) {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;


    }


    
    public static void main(String[] args) {
        Solution solution = new SortColors().new Solution();
        // put your test code here
        
    }
}
